package com.qyer.lxp.product.service.impl;

import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.qyer.lxp.common.framework.contants.ClientErrorMessageEnum;
import com.qyer.lxp.common.framework.contants.QyerContants;
import com.qyer.lxp.common.framework.exception.QyerClientException;
import com.qyer.lxp.common.framework.util.Md5Encrypt;
import com.qyer.lxp.common.framework.util.QyerConfig;
import com.qyer.lxp.common.framework.util.QyerUtils;
import com.qyer.lxp.model.Response;
import com.qyer.lxp.model.product.lastminute.Lastminute_all;
import com.qyer.lxp.model.product.lastminute.Lastminute_category;
import com.qyer.lxp.product.model.ProductParam;
import com.qyer.lxp.product.service.IWebProductService;
import com.qyer.lxp.service.product.IProductService;
import org.mortbay.jetty.security.Credential;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.Map;

/**
 * Created by wangzhen on 16/5/30.
 */
@Path("/")
@Consumes({MediaType.APPLICATION_JSON, MediaType.TEXT_XML})
@Produces({ContentType.APPLICATION_JSON_UTF_8, ContentType.TEXT_XML_UTF_8})
public class WebProductServiceImpl implements IWebProductService {

    @Resource
    private IProductService productService;


    @GET
    @Path("/code/{code}")
    public Lastminute_all getProductInfo(@PathParam("code") String code/*, @Context HttpServletRequest request*/) throws Exception {
        if (RpcContext.getContext().getRequest(HttpServletRequest.class) != null) {
            System.out.println("Client IP address from RpcContext: " + RpcContext.getContext().getRequest(HttpServletRequest.class).getRemoteAddr());
        }
        if (RpcContext.getContext().getResponse(HttpServletResponse.class) != null) {
            System.out.println("Response object from RpcContext: " + RpcContext.getContext().getResponse(HttpServletResponse.class));
        }
        if (QyerUtils.isEmpty(code)) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        return productService.getInfoByProductCode(code);
    }

    @POST
    @Path("/productInfo")
    public Response<Lastminute_all> getProductInfo(ProductParam param) throws Exception {
        Response<Lastminute_all> response = new Response<Lastminute_all>();
        if (null == param) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        String secretKey = QyerConfig.getInstance().getString(QyerContants.LIXING_SECRET_KEY);
        String token = Md5Encrypt.md5("param:" + param.getProductCode() + param.getTimestamp() + param.getSupplierid() + secretKey);

        if (token.equals(param.getToken())) {
            if ("Lixing".equals(param.getSupplierid())) {
                Lastminute_all lastminute_all = productService.getInfoByProductCode(param.getProductCode());
                response.setNameSpace("productInfo");
                response.setData(lastminute_all);
            } else {
                throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_507.getCode());
            }

        } else {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_506.getCode());
        }


        return response;

    }

    @POST
    @Path("/productCategory")
    public Response<Map<String, List<Lastminute_category>>> getProductCategory(ProductParam param) throws Exception {
        Response<Map<String, List<Lastminute_category>>> response = new Response<Map<String, List<Lastminute_category>>>();
        if (null == param) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_400.getCode());
        }
        if (null == param.getProductCode() || QyerUtils.isEmpty(param.getProductCode()) || null == param.getSaleCode() || QyerUtils.isEmpty(param.getSaleCode()) || null == param.getCodeSpecficationId() || QyerUtils.isEmpty(param.getCodeSpecficationId())) {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_508.getCode());
        }

        String secretKey = QyerConfig.getInstance().getString(QyerContants.LIXING_SECRET_KEY);
        String token = Md5Encrypt.md5("param:" + param.getProductCode() + param.getTimestamp() + param.getSupplierid() + secretKey);
        if (token.equals(param.getToken())) {
            if ("Lixing".equals(param.getSupplierid())) {
                Map<String, List<Lastminute_category>> map = productService.getPriceBySaleCode(param.getProductCode(), param.getSaleCode(), param.getCodeSpecficationId());
                response.setNameSpace("productCategory");
                response.setData(map);

            } else {
                throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_507.getCode());
            }
        } else {
            throw new QyerClientException(ClientErrorMessageEnum.CLIENT_RESPONSE_506.getCode());
        }
        return response;

    }


}
